package com.doupi.partner.service.impl;

import com.doupi.partner.dal.SystemConfigMapper;
import com.doupi.partner.dal.constant.DalConstants;
import com.doupi.partner.dal.model.SystemConfig;
import com.doupi.partner.service.SystemConfigService;
import com.doupi.partner.util.Pagination;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemConfigServiceImpl implements SystemConfigService {
	private final SystemConfigMapper systemConfigMapper;

	@Autowired
	public SystemConfigServiceImpl(SystemConfigMapper systemConfigMapper) {
		this.systemConfigMapper = systemConfigMapper;
	}

	public int deleteByPrimaryKey(String key) {
		return systemConfigMapper.deleteByPrimaryKey(key);
	}

	public int insert(SystemConfig record) {
		return systemConfigMapper.insert(record);
	}

	public SystemConfig selectByPrimaryKey(String key) {
		return systemConfigMapper.selectByPrimaryKey(key);
	}

	public int updateByPrimaryKeySelective(SystemConfig record) {
		return systemConfigMapper.updateByPrimaryKeySelective(record);
	}

	public int countSystemConfig(SystemConfig systemConfig) {
		return systemConfigMapper.countSystemConfig(systemConfig);
	}

	public List<SystemConfig> querySystemConfigs(SystemConfig systemConfig, RowBounds rowBounds) {
		return systemConfigMapper.querySystemConfigs(systemConfig, rowBounds);
	}

	public SystemConfig selectSystemConfigForUpdateByPrimaryKey(String key) {
		return systemConfigMapper.selectSystemConfigForUpdateByPrimaryKey(key);
	}

	public Pagination<SystemConfig, SystemConfig> querySystemConfigs(Pagination<SystemConfig, SystemConfig> pagination) {
		int count = countSystemConfig(pagination.getParam());
		pagination.setTotalCount(count);
		if (count > 0) {
			pagination.setRecords(querySystemConfigs(pagination.getParam(), new RowBounds(pagination.getOffset(), pagination.getLimit())));
		}
		return pagination;
	}

	@Override
	public List<SystemConfig> querySystemConfigsByParentKey (String parentKey) {
		SystemConfig systemConfig = new SystemConfig();
		systemConfig.setParentKey(parentKey);
		return querySystemConfigs(systemConfig, DalConstants.MAX_ROW_BOUNDS);
	}
}
